package http

import (
	"encoding/json"
	"fmt"
	"sort"

	"gitee.com/shoyer/util"
	"github.com/spf13/cast"
)

func GetJson(url string, jsonObj interface{}, ops ...*Options) (err error) {
	d, _, err := Get(url, ops...)
	if err != nil {
		return
	}

	err = json.Unmarshal(d, jsonObj)
	if err != nil {
		return fmt.Errorf("GetJson json.Unmarshal: err = %s, res = %s", err, d)
	}

	// if strings.Contains(resp.Header.Get("Content-Type"), "application/json") || util.IsJson(cast.ToString(d)) {
	// 	err = json.Unmarshal(d, jsonObj)
	// } else {
	// 	err = ErrNotJsonString
	// }
	return
}

func PostJson(url string, jsonObj interface{}, ops ...*Options) (err error) {
	if len(ops) > 0 {
		ops[0].SetHeader("Content-Type", "application/json")
	}

	d, _, err := Post(url, ops...)
	if err != nil {
		return
	}

	err = json.Unmarshal(d, jsonObj)
	if err != nil {
		return fmt.Errorf("PostJson json.Unmarshal: err = %s, res = %s", err, d)
	}

	// if strings.Contains(resp.Header.Get("Content-Type"), "application/json") || util.IsJson(cast.ToString(d)) {
	// 	err = json.Unmarshal(d, jsonObj)
	// } else {
	// 	err = ErrNotJsonString
	// }
	return
}

func PutJson(url string, jsonObj interface{}, ops ...*Options) (err error) {
	if len(ops) > 0 {
		ops[0].SetHeader("Content-Type", "application/json")
	}

	d, _, err := Put(url, ops...)
	if err != nil {
		return
	}

	err = json.Unmarshal(d, jsonObj)
	if err != nil {
		return fmt.Errorf("PutJson json.Unmarshal: err = %s, res = %s", err, d)
	}

	// if strings.Contains(resp.Header.Get("Content-Type"), "application/json") || util.IsJson(cast.ToString(d)) {
	// 	err = json.Unmarshal(d, jsonObj)
	// } else {
	// 	err = ErrNotJsonString
	// }

	return
}

func DeleteJson(url string, jsonObj interface{}, ops ...*Options) (err error) {
	if len(ops) > 0 {
		ops[0].SetHeader("Content-Type", "application/json")
	}

	d, _, err := Delete(url, ops...)
	if err != nil {
		return
	}

	err = json.Unmarshal(d, jsonObj)
	if err != nil {
		return fmt.Errorf("DeleteJson json.Unmarshal: err = %s, res = %s", err, d)
	}

	// if strings.Contains(resp.Header.Get("Content-Type"), "application/json") || util.IsJson(cast.ToString(d)) {
	// 	err = json.Unmarshal(d, jsonObj)
	// } else {
	// 	err = ErrNotJsonString
	// }

	return
}

func GetString(url string, ops ...*Options) (s string, err error) {
	d, _, err := Get(url, ops...)
	if err != nil {
		return
	}

	if len(d) == 0 {
		return
	}

	s = cast.ToString(d)
	return
}

func isDebug(ops ...*Options) bool {
	if len(ops) > 0 && ops[0].IsDebug {
		return true
	}

	return false
}

// 将url的queryString按字典序排序后返回
func GetSortedQueryString(queryString string) string {
	if queryString == "" {
		return ""
	}

	pairs := util.Split(queryString, "&")
	if len(pairs) <= 1 {
		return queryString
	}

	m := make(map[string]string)
	keys := make([]string, 0)
	for _, v := range pairs {
		vv := util.Split(v, "=")
		if len(vv) <= 1 {
			m[vv[0]] = ""
		} else {
			m[vv[0]] = vv[1]
		}

		keys = append(keys, vv[0])
	}

	sort.Strings(keys)

	sorted := make([]string, 0)

	for _, v := range keys {
		sorted = append(sorted, fmt.Sprintf("%s=%s", v, m[v]))
	}

	return util.Join(sorted, "&")
}
